using System;
using System.Collections.Generic;
#if (!SILVERLIGHT)
using System.Web;
#endif

namespace QSBusinessLayerCSharp
{
    /// <summary>
    /// PersonMapper would perform all data persistence for the Person business object.
    /// </summary>
    public class PersonMapper
    {
        private static PersonCollection people;

        private static PersonCollection GetPeople()
        {
            if (people == null)
            {
#if (!SILVERLIGHT)
                var context = HttpContext.Current;
                if (context != null)
                {
                    var sessionObject = context.Session["people"];
                    if (sessionObject != null)
                    {
                        people = (PersonCollection)sessionObject;
                    }

                }
#endif
            }
            if (people == null)
            {
                people = GetDefaultPeople();
                TrySaveToSession();
            }
            return people;
        }

        private static void TrySaveToSession()
        {
#if (!SILVERLIGHT)
            var context = HttpContext.Current;
            if (context != null)
            {
                context.Session["people"] = people;
            }
#endif
        }

        private static PersonCollection GetDefaultPeople()
        {

            var person1 = new Person
                                 {
                                     FirstName = "John",
                                     Id = 0,
                                     LastName = "Smith",
                                     Age = 31,
                                     EmailAddress = "John.Smith@b.com",
                                     PhoneNumber = "02-1234-1234",
                                     CardType = CardType.Visa,
                                     CreditCardNumber = "1234567890123456",
                                     OrderTotal = 21.99M,
                                     Gender = Gender.Male
                                 };
            var address1 = new Address();
        	person1.Address = address1;

            var person2 = new Person
                                 {
                                     Id = 1,
                                     FirstName = "Jane",
                                     LastName = "Doe",
                                     Age = 56,
                                     EmailAddress = "Jane.Doe@b.com",
                                     PhoneNumber = "05-4224-7337",
                                     CardType = CardType.Amex,
                                     CreditCardNumber = "123456789012345",
                                     OrderTotal = 56.22M,
                                     Gender = Gender.Female
                                 };
            var address2 = new Address();
			person2.Address = address2;

            var list = new PersonCollection {person1, person2};
            return list;
        }

        public IList<Person> GetAllPeople()
        {
            return new List<Person>(GetPeople());
        }

        public Person GetItem(int id)
        {
            var collection = GetPeople();
            return collection[id];
        }

        public void Save(Person person)
        {
            if (!person.IsValid)
            {
                throw new ArgumentException("Person is not valid.  Save is not allowed.", "person");
            }

            var collection = GetPeople();
            collection.Add(person);
            TrySaveToSession();
        }

        public void Delete(Person person)
        {
            if (!person.IsValid)
            {
                throw new ArgumentException("Person is not valid.  Delete is not allowed.", "person");
            }

            var collection = GetPeople();
            collection.Remove(person);
            TrySaveToSession();
        }
        public void SaveOrUpdate(Person person)
        {
            if (!person.IsValid)
            {
                throw new ArgumentException("Person is not valid.  SaveOrUpdate is not allowed.", "person");
            }
            var collection = GetPeople();
            if (collection.Contains(person))
            {
                collection.Remove(person);
            }
            collection.Add(person);
            TrySaveToSession();
        }

    }
}